#include "../stdafx.h"
#include "Inputs.h"

namespace F16
{
    // Coded from the simulator study document
    namespace ENGINE
    {
        // Constants
        constexpr double MAX_PERCENT_POWER = 100.0;
        constexpr double MIN_PERCENT_POWER = 0.0;
        constexpr double MAX_THRUST = 35000.0; // Updated to reflect F-22's engine thrust

        double percentPower = 0.0;
        double power3 = 0;
        double dt;
        double throttlePosition = 0;
        double throttlePositionLastTime = 0;
        double engine_damage = 0.0;
        double fuel = 0.0;
        double thrust = 0;

        double engine_dynamics(double throttleInput, double mach, double alt, double frameTime)
        {
            // Calculate percent power based on throttle input
            if (throttleInput < 90.0)
            {
                percentPower = throttleInput * 0.8923;
            }
            else
            {
                percentPower = throttleInput * 4.5455 - 354.55;
            }

            // Ensure percentPower is within valid range
            percentPower = limit(percentPower, MIN_PERCENT_POWER, MAX_PERCENT_POWER);
            double power1 = percentPower;
            double power2 = 0.0;
            double power3rate = 0.0;

            // Engine dynamics based on power level
            if (power1 < 50.0)
            {
                if (power3 < 50.0)
                {
                    power2 = power1;
                    if ((power2 - power3) < 40.0)
                    {
                        power3rate = 1.0 * (power2 - power3);
                    }
                    else
                    {
                        power3rate = 0.1 * (power2 - power3);
                    }
                }
                else
                {
                    power2 = 40.0;
                    power3rate = 5.0 * (power2 - power3);
                }
            }
            else
            {
                if (power3 < 50.0)
                {
                    power2 = 60.0;
                    if ((power2 - power3) < 40.0)
                    {
                        power3rate = 1.0 * (power2 - power3);
                    }
                    else
                    {
                        power3rate = 0.1 * (power2 - power3);
                    }
                }
                else
                {
                    power2 = power1;
                    power3rate = 5.0 * (power2 - power3);
                }
            }

            power3 += (power3rate * frameTime);
            power3 = limit(power3, MIN_PERCENT_POWER, MAX_PERCENT_POWER);

            // Calculate thrust based on power and altitude
            double machLimited = limit(mach, 0.2, 2.5);
            double Tidle = (-24976.0 * machLimited + 9091.5) + ((alt / 55000) * 12000.0);
            double Tmil = (-25958.0 * pow(machLimited, 3.0) + 34336.0 * pow(machLimited, 2.0) - 14575.0 * machLimited + 58137.0) + ((alt / 50000.0) * -42000.0);
            double Tmax = (26702.0 * pow(machLimited, 2.0) + 8661.4 * machLimited + 92756.0) + ((alt / 50000.0) * -100000.0);

            double thrust = 0.0;
            if (power3 < 50.0)
            {
                thrust = Tidle + (Tmil - Tidle) * (power3 / 50.0);
            }
            else
            {
                thrust = Tmil + (Tmax - Tmil) * (power3 - 50.0) / 50.0;
            }

            // Adjust thrust for engine damage
            thrust = limit(thrust, 0.0, MAX_THRUST) / (1 + engine_damage * 100);

            return thrust;
        }
    }
}
#pragma once
